
package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.metagranth.service.AdminQuestionService;
import com.metagurukul.metagranth.service.QuestionService;
import com.metagurukul.metagranth.vo.MetaData;


/**
 * Servlet implementation class AdminServlet
 * This servlet will get and retrieve all the question list for the admin panel ,
 * Those questions which have atleast one answer and are currently not closeed.
 * 
 * This will also perform an added functionality of getting the count of total number of questions closed and open 
 * and will return the count on the admin page 
 * @author team MetaGranth
 *
 */
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminServlet() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * this function will retrieve the question list and associated data (metadataList) by calling getAllQuestionsForAdmin
	 * on the service layer.
	 * It will also call the appropriate function for counting the total number of questions closed and open 
	 * and will return the count on the admin home page
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession(true);
		if(session.getAttribute("userId")!=null){
			try{
				//gets the mataDataList for all the Questions to be displayed on admin home page
			ArrayList<MetaData> metaDataList = QuestionService.getAllQuestionsForAdmin();
			request.setAttribute("metaDataList", metaDataList);
			
			// this countCloseQuestion() function will count the total number of questions closed so far.
			int countClosed = AdminQuestionService.countCloseQuestion();
			request.setAttribute("countClosed", countClosed);
			//redirect to the admin home page
			request.getRequestDispatcher("adminhome.jsp").forward(request,response);
		
		} catch(Exception exception) {
			
			request.setAttribute("errormsg", exception.getMessage());
			request.getRequestDispatcher("errorPage.jsp").forward(request, response);
		}
	} else {
		response.sendRedirect("index.jsp");
	}

}
}
